---
title: "Experiment 2a"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r load libraries}
library(readr)
library(reshape2)
library(dplyr)
library(plyr)
library(ggplot2)
library(lmerTest)
library(car)

```

```{r import}
desisl_fulldata <- read_csv("~/Dropbox/Mac/Desktop/Studies/Force/analyses/FINAL/Exp.2/exp2a_data.csv", 
    col_names = FALSE)

```
###Finish cleaning data
```{r clean_data}
#Name the columns
names(desisl_fulldata)<-c("sub","LLH_LHH","had_did","keep_rej","neg_pos","resp")


#Specify which variables are factors are factors
desisl_fulldata$LLH_LHH<-factor(desisl_fulldata$LLH_LHH,labels=c("LLH","LHH"))
desisl_fulldata$had_did<-factor(desisl_fulldata$had_did,labels=c("had","did"))
desisl_fulldata$keep_rej<-factor(desisl_fulldata$keep_rej,labels=c("keep","rej"))
desisl_fulldata$neg_pos<-factor(desisl_fulldata$neg_pos,labels=c("neg","pos"))

#Separate positive and negative reject and keep data
keep_data<-desisl_fulldata[which(desisl_fulldata$keep_rej=="keep"),]
rej_data<-desisl_fulldata[which(desisl_fulldata$keep_rej=="rej"),]
neg_keep_data<-keep_data[which(keep_data$neg_pos=="neg"),]
pos_keep_data<-keep_data[which(keep_data$neg_pos=="pos"),]
neg_rej_data<-rej_data[which(rej_data$neg_pos=="neg"),]
pos_rej_data<-rej_data[which(rej_data$neg_pos=="pos"),]

#reverse score neg responses
neg_keep_data$resp<-6-neg_keep_data$resp
neg_rej_data$resp<-6-neg_rej_data$resp

#group all of the keep and reject data back together
full_keep_data<-rbind(pos_keep_data,neg_keep_data)
full_rej_data<-rbind(pos_rej_data,neg_rej_data)
```

##Descriptives
```{r descriptives}

#get descriptives
full_keep_desc <- ddply(full_keep_data, c("LLH_LHH", "had_did"), summarise,
               N    = length(na.omit(resp)),
               mean = mean(na.omit(resp)),
               sd   = sd(na.omit(resp)),
               se   = sd / sqrt(N)
)
print(full_keep_desc)

#get descriptives
full_rej_desc <- ddply(full_rej_data, c("LLH_LHH", "had_did"), summarise,
               N    = length(na.omit(resp)),
               mean = mean(na.omit(resp)),
               sd   = sd(na.omit(resp)),
               se   = sd / sqrt(N)
)
print(full_rej_desc)

neg_keep_desc <- ddply(neg_keep_data, c("LLH_LHH", "had_did"), summarise,
               N    = length(na.omit(resp)),
               mean = mean(na.omit(resp)),
               sd   = sd(na.omit(resp)),
               se   = sd / sqrt(N)
)
print(neg_keep_desc)

#get descriptives
pos_keep_desc <- ddply(pos_keep_data, c("LLH_LHH", "had_did"), summarise,
               N    = length(na.omit(resp)),
               mean = mean(na.omit(resp)),
               sd   = sd(na.omit(resp)),
               se   = sd / sqrt(N)
)
print(pos_keep_desc)

#get descriptives
neg_rej_desc <- ddply(neg_rej_data, c("LLH_LHH", "had_did"), summarise,
               N    = length(na.omit(resp)),
               mean = mean(na.omit(resp)),
               sd   = sd(na.omit(resp)),
               se   = sd / sqrt(N)
)
print(neg_rej_desc)

#get descriptives
pos_rej_desc <- ddply(pos_rej_data, c("LLH_LHH", "had_did"), summarise,
               N    = length(na.omit(resp)),
               mean = mean(na.omit(resp)),
               sd   = sd(na.omit(resp)),
               se   = sd / sqrt(N)
)
print(pos_rej_desc)

```
###Analyses for manuscript
```{r final analyses}

#create a new variable for rej and keep data that is normal atlernative and abnormal alternative
full_keep_data$norm_cond<-dplyr::recode(full_keep_data$LLH_LHH, LLH="abnormal", LHH="normal")
full_rej_data$norm_cond<-dplyr::recode(full_rej_data$LLH_LHH, LLH="normal", LHH="abnormal")
#combine keep and reject data
full_data<-rbind(full_keep_data,full_rej_data)
#isolate just the had trials
full_had_data<-full_data[which(full_data$had_did=="had"),]

#get descriptives for had data
full_had_desc <- ddply(full_had_data, c("norm_cond"), summarise,
               N    = length(na.omit(resp)),
               mean = mean(na.omit(resp)),
               sd   = sd(na.omit(resp)),
               se   = sd / sqrt(N)
)
print(full_had_desc)

#Run a model with condition and random effect for subject
fullmod <- lme4::lmer(resp ~ norm_cond + (1|sub), data = full_had_data)
summary(fullmod)

#Model dropping condition
mod_NoCond<-lme4::lmer(resp ~ (1|sub), data = full_had_data)
summary(mod_NoCond)

#Compare the two models
anova(fullmod,mod_NoCond)



```






Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
